<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Query extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function query()
    {
    	$code=$this->request->param('m');
    	$pici_id=$this->request->param('p');

    	// var_dump($data);die;

    	$pici_model= new \app\admin\model\Pici;
        $pici=$pici_model->get($pici_id);


        $code_moel=new \app\admin\model\Code;
        $code_moel->setTable("fw_code_100".$pici->enterp_id);
        $code_info=$code_moel
        			->with(['template','pro','agent','noticle','admin'])
        			->where(['code'=>$code])->find();
        if($code_info->status=='hidden' || $pici->status=='hidden'){
        	$this->error("防伪码已被禁用");
        }
        if(!$code_info->template_id){
        	$this->error("防伪码未赋能模板");
        }
        //如果有溯源信息，查询溯源信息
        if($code_info->suyuan_ids!=''){
        	$suyuan_model=new \app\admin\model\Suyuan;
        	$suyuan_ids=explode(',', $code_info->suyuan_ids);
        	$suyuan=$suyuan_model->where('id','IN',$suyuan_ids)->select();
        	$this->assign("suyuan",$suyuan);
        }

        //增加查询次数
        $code_info->hits+=1;
        $code_info->save();


        //替换指定字符
        if($code_info->noticle_id){
            if ($code_info->hits==1){
                $noticle=$code_info->noticle->noticle_first;
            }
            if ($code_info->hits>1 && $code_info->hits<$code_info->noticle->noticle_num){
                $noticle=$code_info->noticle->noticle_not_first;
            }
            if ($code_info->hits>=$code_info->noticle->noticle_num){
                $noticle=$code_info->noticle->noticle_warning;
            }
            $noticle = str_replace("{{hits}}", $code_info->hits, $noticle);
        }else{
            $noticle="您是第".$code_info->hits."次查询。";
        }
        

        $this->assign("pici",$pici);
        $this->assign("noticle",$noticle);
        $this->assign("code_info",$code_info);

        return $this->view->fetch($code_info->template->path);
    }

}
